{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 环境配置"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 安装conda"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--2020-08-18 20:22:58--  https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh\n",
      "正在解析主机 repo.continuum.io (repo.continuum.io)... 104.18.201.79, 104.18.200.79, 2606:4700::6812:c84f, ...\n",
      "正在连接 repo.continuum.io (repo.continuum.io)|104.18.201.79|:443... 已连接。\n",
      "已发出 HTTP 请求，正在等待回应... 301 Moved Permanently\n",
      "位置：https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh [跟随至新的 URL]\n",
      "--2020-08-18 20:23:02--  https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh\n",
      "正在解析主机 repo.anaconda.com (repo.anaconda.com)... 104.16.130.3, 104.16.131.3, 2606:4700::6810:8303, ...\n",
      "正在连接 repo.anaconda.com (repo.anaconda.com)|104.16.130.3|:443... 已连接。\n",
      "已发出 HTTP 请求，正在等待回应... 200 OK\n",
      "长度：93052469 (89M) [application/x-sh]\n",
      "正在保存至: “Miniconda3-latest-Linux-x86_64.sh”\n",
      "\n",
      "16% [=====>                                 ] 15,456,713   246KB/s 剩余 3m 48s   ^C\n"
     ]
    }
   ],
   "source": [
    "# https://blog.csdn.net/weixin_43840215/article/details/89599559\n",
    "!wget -c https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    }
   },
   "outputs": [],
   "source": [
    "!chmod 777 Miniconda3-latest-Linux-x86_64.sh\n",
    "!sh Miniconda3-latest-Linux-x86_64.sh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/bin/bash: conda: 未找到命令\n",
      "/bin/bash: conda: 未找到命令\n",
      "/bin/bash: conda: 未找到命令\n",
      "/bin/bash: conda: 未找到命令\n",
      "/bin/bash: conda: 未找到命令\n",
      "/bin/bash: conda: 未找到命令\n"
     ]
    }
   ],
   "source": [
    "# 上面链接都是https的。然而我在下载的时候报错了。应该把s删掉\n",
    "!conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/\n",
    "!conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/\n",
    "!conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/\n",
    "!conda config --add channels http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/\n",
    "!conda config --set show_channel_urls yes \n",
    "!conda config --get channels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    }
   },
   "outputs": [],
   "source": [
    "# https://blog.csdn.net/zzq060143/article/details/88042075\n",
    "# 请仔细参考这个，添加镜像\n",
    "!conda install pytorch torchvision cudatoolkit=10.2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/root/miniconda3/bin/python'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import sys\n",
    "\n",
    "sys.executable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    }
   },
   "outputs": [],
   "source": [
    "# 由于我是使用conda安装的，默认使用的是conda的base环境。\n",
    "# 所以其他环境是使用不了torch的\n",
    "# 先要激活conda环境，并且退出当前的virtualenv\n",
    "\n",
    "!deactivate # 在当前环境直接输入这个指令即可\n",
    "!source miniconda3/bin/activate\n",
    "!conda jupyter notebook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.DataFrame(np.arange(8).reshape(4,2),columns=['a','b'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "test = pd.DataFrame(np.arange(4,12).reshape(4,2),columns=['c','d'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 可以使用这种方法同时修改训练集和测试集\n",
    "all_data = [data, test]\n",
    "for i in all_data:\n",
    "    i.iloc[:,1] = np.arange(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看电脑的显卡能不能使用\n",
    "torch.cuda.is_available()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 两个重要函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "# dir(torch) dir可以查看有哪些可用的函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "# help(torch.cuda) help告诉我们如何使用这个函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# torch的学习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3],\n",
       "       [4, 5, 6, 7]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch类型数据\n",
    "# numpy类型数据\n",
    "\n",
    "np_data = np.arange(8).reshape(2,4)\n",
    "np_data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在torch中，如果是一个数字，就是0维，**在torch中没有string类型的数据**。如果要表示字符串类型，可以使用one-hot方法\n",
    "\n",
    "然而在实际的生活中，英语单词有上万多个，我们单独靠one-hot，会使得矩阵非常稀疏。而且不能表达出单词的近似程度。\n",
    "\n",
    "所以one-hot编码是有缺陷的，所以人们开发出来其他的方式来编码，编码叫Embedding， 里面有w2v， glove方式"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "floattensor  32\n",
    "\n",
    "doubletensor 64\n",
    "\n",
    "longtensor 64\n",
    "\n",
    "inttensor 32\n",
    "\n",
    "bytetensor 8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'torch.FloatTensor'"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = torch.randn(2,3)\n",
    "a.type() # 返回的是具体的数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Tensor"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回的是基本的数据类型\n",
    "type(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断诗句类型是不是一致\n",
    "isinstance(a, torch.FloatTensor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "deletable": false,
    "editable": false,
    "run_control": {
     "frozen": true
    }
   },
   "outputs": [],
   "source": [
    "# 将数据放到GPU的时候数据类型但是又会发生变化\n",
    "\n",
    "isinstance(data, torch.cuda.FloatTensor)\n",
    "\n",
    "data.cuda() # 将数据放到GPU上\n",
    "\n",
    "isinstance(data, torch.cuda.FloatTensor)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "0维的有什么用呢？通常loss就是0维的，也就是标量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(1.)"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 纬度为0的标量，维度为0就是标量\n",
    "torch.tensor(1.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.tensor(1.).item()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([])"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.tensor(1.).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.tensor(1.).dim()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "维度为1的张量有什么用呢？\n",
    "\n",
    "在神经元中，我们通常要设置偏置。ax+b中b就是。我们用一维张量表示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1])"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1 dim []\n",
    "torch.tensor([1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([1])"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.tensor([1]).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "only one element tensors can be converted to Python scalars",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-95-6daa886b8cad>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtensor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mitem\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m: only one element tensors can be converted to Python scalars"
     ]
    }
   ],
   "source": [
    "torch.tensor([1,1]).item()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-1.0798e-24,  4.5891e-41, -1.5148e+00],\n",
       "        [ 3.0624e-41,         nan,  0.0000e+00]])"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2 dim [[],[]],多个列表嵌套就是2维\n",
    "\n",
    "a = torch.FloatTensor(2,3)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-1.0798e-24,  4.5891e-41, -1.3108e+00]])"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = torch.FloatTensor(1,3) # 注意这是随机生成的\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[-1.0798e-24,  4.5891e-41, -1.5225e+00],\n",
       "         [ 3.0624e-41,  4.4842e-44,  0.0000e+00]]])"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3 dim [[[]],[[]]]\n",
    "# 3 dim有什么用呢？他们的使用场景非常广泛\n",
    "# 比如在nlp中，一句话有10个单词，每个单词用向量表示这句话就可以用2维tensor表示[10, 100]/假如每个单词用100个表示\n",
    "# 假如现在有20句话，用3 dim表示[20,10,100]\n",
    "c = torch.FloatTensor(1,2,3)\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-1.0798e-24,  4.5891e-41, -1.5225e+00],\n",
       "        [ 3.0624e-41,  4.4842e-44,  0.0000e+00]])"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-1.0798e-24,  4.5891e-41, -1.5225e+00])"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 更高维 4dim，5dim\n",
    "# [2,3,29,29]\n",
    "# 上面2表示照片数量，3表示每个照片颜色通道RGB，29*29表示长宽\n",
    "# 注意一个维度，只能用来描述一种特征"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 创建tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 未初始化的tensor，不要直接使用未初始化的数据，输入到其他地方\n",
    "# 未初始化只是为了申请内存，然后赋值给他。\n",
    "# 如果直接将未初始化的数据喂到其他地方。因为值是随机的。有些值是无穷大。容易出现问题\n",
    "\n",
    "torch.FloatTensor(2,2,2) # 未初始化的数据，其内部的值是随机的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.8526, 0.3177, 0.6964],\n",
       "        [0.0450, 0.3220, 0.9346],\n",
       "        [0.5983, 0.3297, 0.9744]])"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 随机初始化\n",
    "torch.rand(3,3) # 初始化的值是在0-1之间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0.4837, 0.7445, 0.2592],\n",
       "         [0.3884, 0.1026, 0.6049],\n",
       "         [0.4994, 0.7637, 0.4178]],\n",
       "\n",
       "        [[0.3952, 0.8937, 0.4947],\n",
       "         [0.4957, 0.4705, 0.9129],\n",
       "         [0.7484, 0.5479, 0.8227]],\n",
       "\n",
       "        [[0.6375, 0.6115, 0.6347],\n",
       "         [0.4693, 0.2609, 0.5884],\n",
       "         [0.2203, 0.5070, 0.4755]]])"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = torch.rand(3,3,3)\n",
    "c = torch.rand_like(a) # 读取一个tensor的shape，然后传给rand like。like相似\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[2, 9],\n",
       "        [3, 9]])"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.randint(1,10,[2,2]) # 在最大和最小值区间生成随机数，第三个参数是指定shape，以列表的形式传入，不包括最大值10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.8897,  1.5054,  0.1210],\n",
       "        [-0.1361,  0.3104, -0.7207],\n",
       "        [ 1.0253, -1.2426,  0.5322]])"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.randn(3,3) # 生成一个正太分布的数据。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0., 0., 0.],\n",
       "        [0., 0., 0.]])"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.full([2,3],0.0,) # 好像只能生成float类型的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(3.)"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.full([],3.) # 创建一个标量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.arange(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.linspace(0,10,11)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 0.0000,  1.1111,  2.2222,  3.3333,  4.4444,  5.5556,  6.6667,  7.7778,\n",
       "         8.8889, 10.0000])"
      ]
     },
     "execution_count": 137,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.linspace(0,10,10) # 等份切"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 1., 1.],\n",
       "        [1., 1., 1.],\n",
       "        [1., 1., 1.]])"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.ones(3,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 0., 0.],\n",
       "        [0., 1., 0.],\n",
       "        [0., 0., 1.]])"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.eye(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0., 0., 0.],\n",
       "        [0., 0., 0.],\n",
       "        [0., 0., 0.]])"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.zeros(3,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([2, 4, 3, 1, 8, 0, 7, 6, 9, 5])"
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.randperm(10) # 生成一个随机的序列"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 索引和切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0.9975, 0.8395],\n",
       "         [0.4927, 0.9900],\n",
       "         [0.3013, 0.9192]],\n",
       "\n",
       "        [[0.7212, 0.0430],\n",
       "         [0.5839, 0.6931],\n",
       "         [0.6613, 0.2377]]])"
      ]
     },
     "execution_count": 145,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = torch.rand(2,3,2)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.9975, 0.8395],\n",
       "        [0.4927, 0.9900],\n",
       "        [0.3013, 0.9192]])"
      ]
     },
     "execution_count": 146,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[0] # 索引的是第0个3，2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.9975, 0.8395])"
      ]
     },
     "execution_count": 149,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[0,0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(0.8395)"
      ]
     },
     "execution_count": 148,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[0,0,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[0.9975, 0.8395],\n",
      "        [0.4927, 0.9900],\n",
      "        [0.3013, 0.9192]])\n",
      "tensor([[0.7212, 0.0430],\n",
      "        [0.5839, 0.6931],\n",
      "        [0.6613, 0.2377]])\n"
     ]
    }
   ],
   "source": [
    "for each in data: # 居然可以遍历\n",
    "    print(each)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0.9975, 0.8395],\n",
       "         [0.4927, 0.9900],\n",
       "         [0.3013, 0.9192]]])"
      ]
     },
     "execution_count": 153,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片\n",
    "data[:1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 154,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[:1].dim() # 依旧是三维的 # 这里是对第一个通道索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0.9975, 0.8395]]])"
      ]
     },
     "execution_count": 157,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[:1,:1] # 还可以对第二个通道索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0.3013, 0.9192]]])"
      ]
     },
     "execution_count": 158,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[:1,-1:] # -1开始表示只选取末尾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0.9975, 0.8395],\n",
       "         [0.4927, 0.9900],\n",
       "         [0.3013, 0.9192]]])"
      ]
     },
     "execution_count": 160,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[:1:2,:,:] # 甚至可以隔行采样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0.9250, 0.9249, 0.6874],\n",
       "         [0.3065, 0.2696, 0.8503],\n",
       "         [0.0577, 0.7079, 0.6500]],\n",
       "\n",
       "        [[0.1635, 0.2214, 0.2830],\n",
       "         [0.1326, 0.0176, 0.5144],\n",
       "         [0.7082, 0.1147, 0.5520]],\n",
       "\n",
       "        [[0.0726, 0.4157, 0.1398],\n",
       "         [0.6775, 0.5979, 0.3828],\n",
       "         [0.5380, 0.3950, 0.5863]]])"
      ]
     },
     "execution_count": 162,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = torch.rand(3,3,3)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "index_select() received an invalid combination of arguments - got (int, list), but expected one of:\n * (name dim, Tensor index)\n      didn't match because some of the arguments have invalid types: (\u001b[31;1mint\u001b[0m, \u001b[31;1mlist\u001b[0m)\n * (int dim, Tensor index)\n      didn't match because some of the arguments have invalid types: (\u001b[32;1mint\u001b[0m, \u001b[31;1mlist\u001b[0m)\n",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-163-0c20a8b73951>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mdata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex_select\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: index_select() received an invalid combination of arguments - got (int, list), but expected one of:\n * (name dim, Tensor index)\n      didn't match because some of the arguments have invalid types: (\u001b[31;1mint\u001b[0m, \u001b[31;1mlist\u001b[0m)\n * (int dim, Tensor index)\n      didn't match because some of the arguments have invalid types: (\u001b[32;1mint\u001b[0m, \u001b[31;1mlist\u001b[0m)\n"
     ]
    }
   ],
   "source": [
    "data.index_select(0,[0,2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 1.,  2.,  3.,  4.],\n",
       "        [ 5.,  6.,  7.,  8.],\n",
       "        [ 9., 10., 11., 12.]])"
      ]
     },
     "execution_count": 172,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.select_index用法\n",
    "test = torch.linspace(1,12, 12).view(3,4)\n",
    "test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 175,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 1.,  2.],\n",
       "        [ 5.,  6.],\n",
       "        [ 9., 10.]])"
      ]
     },
     "execution_count": 175,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test.index_select(1, torch.tensor([0,1])) # https://blog.csdn.net/g_blink/article/details/102854188\n",
    "\n",
    "# 参数说明：第一个参数0表示按行索引，1表示按列索引。第二个参数传入一个tensor，表示传入的index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 178,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 1.,  2.,  3.,  4.],\n",
       "        [ 5.,  6.,  7.,  8.],\n",
       "        [ 9., 10., 11., 12.]])"
      ]
     },
     "execution_count": 178,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test[...] # 三个句号表示所有维度都取。:::替代这个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([5., 6., 7., 8.])"
      ]
     },
     "execution_count": 177,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test[1,...] # 可以先给定一个维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 1.,  3.],\n",
       "        [ 5.,  7.],\n",
       "        [ 9., 11.]])"
      ]
     },
     "execution_count": 179,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test[...,::2] # 也可以这样混合着使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 1.3531,  0.2694, -0.6561,  0.3088],\n",
       "        [ 0.9426, -0.3599, -0.1596, -1.0377],\n",
       "        [-0.4034,  1.9707,  0.0576,  0.8735]])"
      ]
     },
     "execution_count": 181,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 设置阈值，将大于阈值的位置设置为1\n",
    "\n",
    "x = torch.randn(3,4)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ True, False, False, False],\n",
       "        [ True, False, False, False],\n",
       "        [False,  True, False,  True]])"
      ]
     },
     "execution_count": 182,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.ge(0.5) # 设置阈值，大于阈值的返回true"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## tensor维度变换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# view，reshape\n",
    "# 这里举例为minist手写数据集。4表示4张图片，3表示颜色只有RGB。28*28表示长宽\n",
    "test = torch.rand(4,3,28,28) # rand生成的数是在0-1之间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 193,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 3, 28, 28])"
      ]
     },
     "execution_count": 193,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 198,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 第一个view操作，合并维度\n",
    "\n",
    "res = test.view(4,3*28*28) # 把后面几个维度合并，特别适合神经网络中的全连接层"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 196,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 2352])"
      ]
     },
     "execution_count": 196,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res.shape"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[1.1157e-01, 5.1504e-01, 2.8517e-01,  ..., 4.9745e-01,\n",
       "           8.8682e-01, 4.4332e-01],\n",
       "          [4.1356e-01, 7.2256e-01, 2.5266e-01,  ..., 8.3731e-01,\n",
       "           4.6646e-01, 3.9150e-01],\n",
       "          [9.5687e-01, 7.2007e-01, 3.8216e-01,  ..., 6.4168e-01,\n",
       "           1.9305e-01, 1.1146e-02],\n",
       "          ...,\n",
       "          [1.0034e-03, 4.7412e-01, 9.3960e-01,  ..., 1.9311e-01,\n",
       "           1.5390e-01, 7.3776e-01],\n",
       "          [9.6935e-01, 5.2568e-01, 6.0228e-01,  ..., 7.0160e-01,\n",
       "           3.8837e-01, 4.4716e-01],\n",
       "          [1.7583e-01, 4.3682e-01, 9.9142e-02,  ..., 9.1282e-01,\n",
       "           1.6332e-01, 5.3109e-01]],\n",
       "\n",
       "         [[9.5560e-01, 5.4060e-01, 7.5033e-01,  ..., 5.1605e-01,\n",
       "           4.8237e-02, 3.5238e-01],\n",
       "          [9.0811e-01, 5.1835e-01, 3.1485e-01,  ..., 9.7172e-01,\n",
       "           4.1515e-01, 4.6730e-01],\n",
       "          [9.0991e-01, 1.3199e-01, 1.2908e-01,  ..., 7.0284e-02,\n",
       "           4.4414e-01, 9.0029e-01],\n",
       "          ...,\n",
       "          [2.0001e-01, 2.4140e-01, 8.7747e-01,  ..., 3.8499e-01,\n",
       "           6.6839e-01, 9.5840e-01],\n",
       "          [3.1856e-01, 7.5449e-01, 7.8377e-01,  ..., 9.7953e-01,\n",
       "           6.1748e-02, 4.2759e-02],\n",
       "          [6.5346e-01, 9.3412e-01, 2.5782e-01,  ..., 2.9316e-01,\n",
       "           7.4467e-01, 6.4488e-01]],\n",
       "\n",
       "         [[2.2932e-01, 8.4955e-01, 4.7205e-01,  ..., 1.5856e-01,\n",
       "           1.7956e-01, 9.4925e-01],\n",
       "          [2.2838e-01, 4.3289e-01, 3.7162e-01,  ..., 2.4201e-01,\n",
       "           5.3294e-01, 4.8109e-01],\n",
       "          [5.8513e-02, 4.1759e-01, 4.8680e-01,  ..., 6.0206e-01,\n",
       "           2.2927e-02, 2.4803e-01],\n",
       "          ...,\n",
       "          [6.2728e-01, 5.5712e-02, 7.2704e-01,  ..., 6.2339e-01,\n",
       "           3.6696e-01, 3.5457e-01],\n",
       "          [9.7132e-01, 5.8574e-01, 9.3136e-01,  ..., 5.3994e-02,\n",
       "           9.4819e-01, 1.6131e-02],\n",
       "          [6.2080e-02, 6.5946e-01, 8.6231e-01,  ..., 7.6918e-01,\n",
       "           9.2329e-01, 2.7516e-02]]],\n",
       "\n",
       "\n",
       "        [[[7.9713e-01, 8.1343e-01, 7.5378e-01,  ..., 8.6076e-01,\n",
       "           3.3079e-01, 4.7705e-01],\n",
       "          [8.3138e-01, 8.0922e-01, 6.1358e-01,  ..., 7.7191e-01,\n",
       "           6.9551e-01, 2.4655e-01],\n",
       "          [6.5769e-01, 4.8201e-01, 8.4894e-01,  ..., 3.3558e-01,\n",
       "           2.0052e-02, 6.0655e-01],\n",
       "          ...,\n",
       "          [5.0237e-01, 5.4568e-01, 8.4785e-01,  ..., 9.5164e-01,\n",
       "           9.5112e-01, 9.9475e-01],\n",
       "          [1.3950e-01, 3.7782e-01, 3.0966e-01,  ..., 2.0721e-01,\n",
       "           9.2307e-01, 6.9030e-01],\n",
       "          [5.9886e-01, 6.1185e-01, 9.5709e-01,  ..., 4.4663e-01,\n",
       "           4.1839e-01, 9.8743e-01]],\n",
       "\n",
       "         [[1.3688e-01, 1.1637e-01, 7.9996e-01,  ..., 3.1220e-01,\n",
       "           9.8527e-01, 2.7667e-01],\n",
       "          [1.3729e-01, 7.5305e-01, 9.2690e-01,  ..., 4.5470e-01,\n",
       "           1.7007e-02, 9.3107e-01],\n",
       "          [7.6963e-01, 5.4870e-01, 7.9069e-01,  ..., 2.0734e-01,\n",
       "           6.8133e-01, 8.3307e-01],\n",
       "          ...,\n",
       "          [8.8776e-01, 1.8566e-02, 7.4821e-01,  ..., 7.9853e-01,\n",
       "           9.2306e-01, 9.6694e-01],\n",
       "          [3.7411e-01, 3.1567e-01, 2.4247e-01,  ..., 4.9480e-01,\n",
       "           3.3200e-01, 7.2635e-01],\n",
       "          [3.9201e-01, 4.0931e-01, 1.5540e-01,  ..., 4.8696e-01,\n",
       "           8.5217e-01, 3.5479e-01]],\n",
       "\n",
       "         [[8.2707e-01, 4.1617e-01, 1.0816e-01,  ..., 1.1996e-01,\n",
       "           9.5686e-01, 3.7425e-01],\n",
       "          [4.4491e-01, 5.0555e-01, 5.4755e-01,  ..., 5.5350e-01,\n",
       "           8.6497e-01, 9.9594e-01],\n",
       "          [9.2001e-01, 2.0231e-01, 5.1691e-01,  ..., 1.4318e-01,\n",
       "           5.5521e-02, 6.2908e-01],\n",
       "          ...,\n",
       "          [4.6925e-01, 2.3000e-01, 5.4127e-01,  ..., 1.8021e-01,\n",
       "           2.4649e-02, 3.8159e-01],\n",
       "          [3.2830e-01, 9.6212e-01, 1.6157e-01,  ..., 3.2269e-01,\n",
       "           7.1558e-01, 2.0334e-02],\n",
       "          [8.1936e-01, 7.4368e-01, 3.0956e-01,  ..., 2.1267e-01,\n",
       "           4.5650e-01, 8.3958e-02]]],\n",
       "\n",
       "\n",
       "        [[[6.0959e-01, 6.4703e-02, 9.4654e-01,  ..., 8.4014e-01,\n",
       "           5.5122e-01, 2.7369e-01],\n",
       "          [6.8772e-01, 6.8297e-01, 4.6219e-01,  ..., 1.2011e-01,\n",
       "           6.8671e-01, 6.6632e-01],\n",
       "          [4.9846e-01, 9.8536e-01, 9.5387e-01,  ..., 9.9714e-02,\n",
       "           2.7782e-01, 8.8437e-01],\n",
       "          ...,\n",
       "          [4.9384e-01, 2.6367e-01, 7.5679e-01,  ..., 1.5595e-01,\n",
       "           8.1734e-01, 3.1631e-01],\n",
       "          [9.7448e-01, 9.8366e-01, 5.9597e-02,  ..., 3.5319e-01,\n",
       "           7.0486e-01, 8.6085e-01],\n",
       "          [2.7091e-01, 1.7917e-01, 1.3208e-01,  ..., 8.4366e-01,\n",
       "           2.1338e-01, 8.3381e-01]],\n",
       "\n",
       "         [[1.7004e-01, 4.1996e-01, 5.4219e-01,  ..., 8.3427e-01,\n",
       "           9.7204e-01, 8.4009e-01],\n",
       "          [6.7205e-01, 6.3793e-01, 4.0409e-02,  ..., 8.8199e-01,\n",
       "           8.4595e-01, 7.7836e-01],\n",
       "          [2.7641e-01, 8.5021e-01, 9.5059e-01,  ..., 5.3873e-01,\n",
       "           6.1885e-01, 7.1154e-01],\n",
       "          ...,\n",
       "          [6.5123e-02, 4.5948e-01, 9.3379e-02,  ..., 7.4096e-01,\n",
       "           1.6802e-01, 2.4682e-01],\n",
       "          [8.3536e-01, 6.2844e-01, 4.4553e-01,  ..., 4.9666e-01,\n",
       "           5.1710e-01, 3.4362e-02],\n",
       "          [5.4869e-01, 4.6865e-01, 3.3470e-01,  ..., 7.0984e-01,\n",
       "           3.4083e-01, 1.3736e-01]],\n",
       "\n",
       "         [[5.6851e-01, 6.4594e-01, 1.6079e-01,  ..., 8.1828e-01,\n",
       "           8.5833e-01, 7.3011e-01],\n",
       "          [7.4309e-01, 6.0341e-01, 3.4625e-01,  ..., 2.4012e-01,\n",
       "           1.2524e-01, 1.9733e-01],\n",
       "          [8.4307e-01, 5.7827e-01, 3.6979e-01,  ..., 2.2358e-01,\n",
       "           1.5837e-01, 1.1465e-01],\n",
       "          ...,\n",
       "          [5.6174e-01, 9.9355e-01, 7.2320e-01,  ..., 8.8616e-01,\n",
       "           5.1463e-01, 6.8255e-01],\n",
       "          [7.7427e-01, 3.3139e-01, 3.0274e-01,  ..., 4.4584e-01,\n",
       "           4.2054e-01, 1.0410e-01],\n",
       "          [8.4675e-01, 8.2422e-01, 6.7722e-01,  ..., 7.4768e-01,\n",
       "           9.1797e-01, 1.1163e-01]]],\n",
       "\n",
       "\n",
       "        [[[2.8185e-01, 2.7215e-02, 8.3081e-01,  ..., 1.5772e-01,\n",
       "           3.8139e-01, 3.1223e-01],\n",
       "          [2.2525e-01, 8.9919e-01, 4.8714e-01,  ..., 3.1468e-01,\n",
       "           4.6501e-01, 7.0845e-01],\n",
       "          [6.4815e-01, 7.8491e-02, 1.9715e-01,  ..., 4.2355e-02,\n",
       "           6.4859e-01, 8.3341e-01],\n",
       "          ...,\n",
       "          [6.6631e-01, 7.1648e-01, 9.2899e-01,  ..., 9.3120e-01,\n",
       "           1.7272e-01, 6.8841e-02],\n",
       "          [9.0251e-01, 3.8536e-01, 5.7835e-01,  ..., 2.7185e-01,\n",
       "           1.6138e-01, 5.9002e-01],\n",
       "          [7.4196e-01, 5.0490e-01, 3.1956e-01,  ..., 8.6216e-01,\n",
       "           7.5172e-01, 8.1483e-01]],\n",
       "\n",
       "         [[4.1914e-04, 4.0124e-01, 1.7917e-01,  ..., 2.5601e-01,\n",
       "           5.4727e-01, 9.1332e-01],\n",
       "          [9.9966e-01, 8.2233e-01, 3.4543e-02,  ..., 8.6637e-02,\n",
       "           7.9122e-01, 3.8539e-01],\n",
       "          [6.4904e-01, 7.3242e-01, 8.6514e-01,  ..., 4.3794e-01,\n",
       "           5.1991e-01, 5.1430e-01],\n",
       "          ...,\n",
       "          [5.8176e-01, 3.9932e-03, 2.0424e-01,  ..., 7.3792e-01,\n",
       "           2.9250e-01, 4.7707e-02],\n",
       "          [7.3663e-01, 2.7570e-01, 1.7928e-01,  ..., 9.9682e-01,\n",
       "           9.0516e-01, 9.6487e-01],\n",
       "          [5.1066e-01, 1.1041e-01, 8.1966e-01,  ..., 5.4880e-01,\n",
       "           5.2681e-01, 8.0532e-01]],\n",
       "\n",
       "         [[4.4486e-02, 4.8150e-02, 7.7939e-01,  ..., 9.3061e-01,\n",
       "           1.9046e-02, 1.8157e-01],\n",
       "          [5.8455e-01, 4.6559e-01, 5.2204e-01,  ..., 5.8551e-01,\n",
       "           6.4057e-01, 3.9016e-01],\n",
       "          [8.8450e-02, 6.7665e-02, 7.8146e-01,  ..., 3.5359e-01,\n",
       "           2.7929e-01, 4.9779e-01],\n",
       "          ...,\n",
       "          [4.4591e-01, 1.9078e-01, 3.8100e-01,  ..., 1.0603e-01,\n",
       "           6.4835e-02, 6.7137e-01],\n",
       "          [6.0473e-01, 9.5546e-01, 7.5000e-01,  ..., 9.5342e-01,\n",
       "           8.3828e-01, 3.4164e-01],\n",
       "          [7.3169e-01, 5.2443e-01, 5.9326e-01,  ..., 9.1829e-01,\n",
       "           9.9309e-01, 2.1434e-01]]]])"
      ]
     },
     "execution_count": 199,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res.view(4,3,28,28)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 将array转化成tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 1, 2, 3],\n",
       "        [4, 5, 6, 7]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch_data = torch.from_numpy(np_data)\n",
    "torch_data # 将np数据转化成tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Tensor"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(torch_data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 将tensor转化成array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "tensor_to_array = torch_data.numpy()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 一些常用函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "data1 = [1,9]\n",
    "data2 = [[1,2],[3,4]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "mean(): argument 'input' (position 1) must be Tensor, not list",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-24-3da3b29173b1>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtorch\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m: mean(): argument 'input' (position 1) must be Tensor, not list"
     ]
    }
   ],
   "source": [
    "torch.mean(data1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.0"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(data1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 在torch和numpy中，前者最好先将数据转化成tensor的形式，后者最好转化成array的形式\n",
    "# 这样转换后，才好通过运算\n",
    "array1 = np.array(data1)\n",
    "array2 = np.array(data2)\n",
    "tensor1 = torch.FloatTensor(data1)\n",
    "tensor2 = torch.FloatTensor(data2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(3.)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.mean(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 0.8415,  0.4121, -0.8415,  0.1411])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.sin(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1., 9., 1., 3.])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.abs(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 矩阵相乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 7, 10],\n",
       "       [15, 22]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "array2.dot(array2) # 矩阵的乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([19, 39])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "array2.dot(array1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1, 18],\n",
       "       [ 3, 36]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "array1*array2 # 甚至可以按位运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1., 9.])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tensor1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 2.],\n",
       "        [3., 4.]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tensor2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(82.)"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tensor1.dot(tensor1) # 1*1+9*9"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 使用torch完成Minist手写数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch import nn\n",
    "from torch.nn import functional as F\n",
    "from torch import optim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torchvision\n",
    "\n",
    "from matplotlib import pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "#加载数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "batch_size = 500\n",
    "\n",
    "train_loader = torch.utils.data.DataLoader(\n",
    "    torchvision.datasets.MNIST('mnist data', train=True, download=True, transform=torchvision.transforms.Compose(\n",
    "        [torchvision.transforms.ToTensor(),torchvision.transforms.Normalize((0.13,),(0.3,))]\n",
    "    )),\n",
    "    batch_size=batch_size,\n",
    "    shuffle=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_loader = torch.utils.data.DataLoader(\n",
    "    torchvision.datasets.MNIST('mnist data', train=False, download=True, transform=torchvision.transforms.Compose(\n",
    "        [torchvision.transforms.ToTensor(),torchvision.transforms.Normalize((0.13,),(0.3,))]\n",
    "    )),\n",
    "    batch_size=batch_size,\n",
    "    shuffle=False,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "i,j = next(iter(train_loader))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([500, 1, 28, 28])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i.shape\n",
    "# 每个batch有500个数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([500])"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "j.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Net(nn.Module):\n",
    "    def __init__(self):\n",
    "        super(Net,self).__init__()\n",
    "        # 一共有三层网络，输入是28*28\n",
    "        # 第二层是256\n",
    "        # 第三层是64\n",
    "        # 最后输出是10，因为有0-9个数字。第二层和第三层的神经元数都是自己设定的\n",
    "        self.func1 = nn.Linear(28*28, 256)\n",
    "        self.func2 = nn.Linear(256, 64)\n",
    "        self.func3 = nn.Linear(64, 10)\n",
    "        \n",
    "    def forward(self, x):\n",
    "        # x:[batch, 1,28,28]\n",
    "        # h1 = relu(xw1+b1)\n",
    "        x = F.relu(self.func1(x))\n",
    "        x = F.relu(self.func2(x))\n",
    "        x = self.func3(x)\n",
    "        \n",
    "        return x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([500, 1, 28, 28]) torch.Size([500])\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'one_hot' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-80-d94253743052>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      7\u001b[0m         \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mview\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m28\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0;36m28\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      8\u001b[0m         \u001b[0mout\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnet\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m         \u001b[0my_onehot\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mone_hot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0my\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     10\u001b[0m         \u001b[0mloss\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mF\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmes_loss\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mout\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my_onehot\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     11\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mNameError\u001b[0m: name 'one_hot' is not defined"
     ]
    }
   ],
   "source": [
    "net = Net()\n",
    "optimizer = optim.SGD(net.parameters(), lr=0.01, momentum = 0.9)\n",
    "\n",
    "for epoch in range(3): # 对数据集，迭代3遍\n",
    "    for batch_index, (x,y) in enumerate(train_loader): # 开始对数据集的每个batch迭代第一遍\n",
    "        print(x.shape,y.shape)\n",
    "        x = x.view(x.size(0), 28*28)\n",
    "        out = net(x)\n",
    "        y_onehot = one_hot(y)\n",
    "        loss = F.mes_loss(out, y_onehot)\n",
    "        \n",
    "        loss.backward()\n",
    "        \n",
    "        optimizer.step()\n",
    "        \n",
    "        if batch_inde % 10 == 0:\n",
    "            print(loss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "171px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
